package com.SFA.OrderManagement;

import java.lang.ref.SoftReference;
import java.util.List;
import java.util.concurrent.ExecutorService;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.FrameWork.Common.BitmapManager;
import com.FrameWork.Entity.ImagesEntity;
import com.FrameWork.Entity.ProductEntity;
import com.FrameWork.Utils.Utils;
import com.SFA.R;
import com.SFA.ProductShow.ProductPhotoList;

public class ProductView extends LinearLayout implements OnClickListener,Runnable {



	private ImageView btnImage;
	private Button btnSelect;
	private TextView txtTitle,txtPrice;
	private ProductEntity entity;


	public ISelectProduct OnSelectProduct;
	public ProductView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Init(context);
	}

	public ProductView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Init(context);
	}

	public ProductView(Context context) {
		super(context);
		Init(context);
	}

	private void Init( Context context)
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		inflater.inflate(R.layout.product_view, this);
		txtTitle = (TextView) this.findViewById(R.id.txtTitle);
		txtPrice = (TextView) this.findViewById(R.id.txtPrice);
		btnImage =  (ImageView) this.findViewById(R.id.btnImage);
		btnSelect=  (Button) this.findViewById(R.id.btnSelect);
		btnSelect.setOnClickListener(this);
		setWillNotDraw(false);
	}

	@Override
	public void onClick(View v) {
		entity.Checked =!entity.Checked;
		if(entity.Checked)
		{
			btnSelect.setText(R.string.CANCEL);
			btnSelect.setBackgroundResource(R.drawable.mydate_ok);
		}
		else
		{
			btnSelect.setText(R.string.label_select);
			btnSelect.setBackgroundResource(R.drawable.mydate_cancel);
		}

		//触发通知事件
		if(OnSelectProduct != null)
		{
			OnSelectProduct.Select(entity);
		}
	}

	private int size;
	public void SetEntity(ProductEntity entity,int size)
	{
		if(entity == null)
			return;
		this.size=size;
		this.entity= entity;
		if(entity.Checked)
		{
			btnSelect.setText(R.string.CANCEL);
			btnSelect.setBackgroundResource(R.drawable.mydate_ok);
		}
		else
		{
			btnSelect.setText(R.string.label_select);
			btnSelect.setBackgroundResource(R.drawable.mydate_cancel);
		}

		txtTitle.setText("[" +  entity.CODE + "]" + entity.SHORTDESCRIPTION);
		txtPrice.setText(entity.PIECE_PRICE + "");
		new Handler().post(this);
	}



	@Override
	public void run()
	{
		//获取该产品的第一张照片
		List<ImagesEntity> list = ProductPhotoList.getPhotoInfoList(getContext(), Integer.parseInt(entity.ID));
		for(ImagesEntity image : list)
		{
			if(Utils.Exist(BitmapManager.DIR + "/" + image.getName()))
			{
				BitmapManager.INSTANCE.loadLocalBitmap(BitmapManager.DIR + "/" + image.getName(), btnImage, size,size,ScaleType.FIT_XY);
				break;
			}
		}
		list = null;
	}

	public interface ISelectProduct
	{
		void Select(ProductEntity entity);
	}

}
